package net.zoneland.greenleaf.servlet;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;

import net.zoneland.greenleaf.GLOBAL;
import net.zoneland.greenleaf.common.worktimehelper.HolidayManager;
import net.zoneland.greenleaf.common.worktimehelper.TimeForDayManager;
import net.zoneland.greenleaf.common.worktimehelper.TimeManager;
import net.zoneland.greenleaf.system.entity.Sys_holidayconf;
import net.zoneland.greenleaf.system.service.Sys_configServiceI;
import net.zoneland.greenleaf.system.service.Sys_holidayconfServiceI;
import net.zoneland.greenleaf.timer.task.TaskUtils;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class FrameworkInitLinstener extends HttpServlet implements ServletContextListener {
	
	private static final long serialVersionUID = 1L;
	
	public static Logger logger = Logger.getLogger(FrameworkInitLinstener.class);
	 
	public void contextDestroyed(ServletContextEvent arg0) {
		logger.info("服务正在注销，容器正在销毁......");
		logger.info("正在获取WebApplicationContext对象......");
		logger.info("正在尝试停止所有的定时任务......");
		TaskUtils taskUtils = new TaskUtils();
		try{
			taskUtils.stopAllTask();
		}catch(Exception e){
			logger.error("系统在尝试停止所有的定时任务时发生异常。", e);
		}
		logger.info("服务已经成功停止。");
	}
	public void contextInitialized(ServletContextEvent arg0) {
		logger.info("应用程序服务正在尝试启动......");	
		logger.info("应用服务容器准备获取Spring WebApplicationContext对象......");
		WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());	
		
		/**
		 * 初始化所有的常量
		 */
		GLOBAL.springContext = springContext;
		GLOBAL.servletContext = arg0.getServletContext();
		
		/**
		 * 尝试根据节假日配置，休息时间配置来初始化时间管理组件
		 * 时间管理工具可以方便地计算工作时间
		 */	
		logger.info("系统正在组织时间管理组件.......");
		Sys_holidayconfServiceI holidayconfService = (Sys_holidayconfServiceI)springContext.getBean("sys_holidayconfService");
		Sys_configServiceI configService = (Sys_configServiceI)springContext.getBean("sys_configService");
		
		Map<String, Object> paramMap = new HashMap<>();	
		logger.debug("时间管理组件初始化，查询所有的节假日......");
		paramMap.put("type", "0".split(","));
		List<Sys_holidayconf> holidayList = holidayconfService.getList(paramMap);
		logger.debug("时间管理组件初始化，查询所有的工作日......");
		paramMap.put("type", "1".split(","));
		List<Sys_holidayconf> workdayList = holidayconfService.getList(paramMap);
		logger.debug("时间管理组件初始化，查询所有的休息时段......");
		paramMap.put("type", "2".split(","));
		List<Sys_holidayconf> forRestTimeQuantumOfDay_List = holidayconfService.getList(paramMap);		
		logger.debug("时间管理组件初始化，组织HolidayMaager对象......");
		HolidayManager holidayManager = new HolidayManager( holidayList, workdayList );
		logger.debug("时间管理组件初始化，查询上班时间......");
		String worktime_start = configService.getConfigByCode("WORKTIME_START");
		logger.debug("时间管理组件初始化，查询下班时间......");
		String worktime_end = configService.getConfigByCode("WORKTIME_END");
		logger.debug("时间管理组件初始化，组织TimeForDayManager对象......");
		TimeForDayManager timeForDayManager = new TimeForDayManager( worktime_start, worktime_end, forRestTimeQuantumOfDay_List );		
		TimeManager timeManager = new TimeManager( holidayManager, timeForDayManager );
		logger.info("时间管理组件TimeManager成功生成。");	
		
		GLOBAL.timeManager = timeManager;
		
		/**
		 * 开始启动所有的定时任务
		 */
		TaskUtils taskUtils = new TaskUtils();
		logger.info("应用程序正在尝试加载所有的定时任务......");
		taskUtils.runAllTask();
		
		logger.info("应用服务已经成功启动。");
	}
}
